package com.jianguozh.difs.support.service.org.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jianguozh.difs.support.service.constant.enums.OrgEnum;
import com.jianguozh.difs.support.service.org.entity.Catering;
import com.jianguozh.difs.support.service.org.entity.Corporate;
import com.jianguozh.difs.support.service.org.mapper.CorporateMapper;
import com.jianguozh.difs.support.service.org.service.ICateringService;
import com.jianguozh.difs.support.service.org.service.ICorporateService;
import com.jianguozh.difs.support.service.sys.entity.User;
import com.jianguozh.difs.support.service.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 餐饮主体 服务实现类
 * </p>
 *
 * @author hanrs
 * @since 2022-03-17
 */
@Service
public class CorporateServiceImpl extends ServiceImpl<CorporateMapper, Corporate> implements ICorporateService {

    @Autowired
    IUserService userService;

    @Autowired
    ICateringService cateringService;


    @Override
    public boolean support(OrgEnum orgEnum) {
        return OrgEnum.CORPORATE == orgEnum;
    }

    @Override
    public List listOrgName(List orgList) {
        List<Corporate> list = (List<Corporate>) orgList;
        return list.stream().map(Corporate::getCorporateName).collect(Collectors.toList());
    }

    @Override
    public User getDefaultManager(String orgId) {
        return userService.getDefaultManager(orgId);
    }

    @Override
    @Transactional
    public boolean removeById(Serializable id) {
        List<String> cateringIds = cateringService.list(new LambdaQueryWrapper<Catering>().eq(Catering::getCorporateId, id)).stream().map(Catering::getId).collect(Collectors.toList());
        boolean b1 = cateringService.removeByIds(cateringIds);
        boolean b2 = super.removeById(id);
        return b1 && b2;
    }

    @Override
    @Transactional
    public boolean removeByIds(Collection<?> list) {
        List<String> cateringIds = cateringService.list(new LambdaQueryWrapper<Catering>().in(Catering::getCorporateId, list)).stream().map(Catering::getId).collect(Collectors.toList());
        boolean b1 = cateringService.removeByIds(cateringIds);
        boolean b2 = super.removeByIds(list);
        return b1 && b2;
    }

}
